package com.rongxun.hbwallet.ui.activities;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.mvp.contract.ILogin;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.LoginPresenter;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.utils.Validator;
import com.rongxun.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/9 9:55
 * 描述
 */
public class LoginActivity extends MvpActivity<ILogin.IView, LoginPresenter> implements ILogin.IView, TextWatcher {
    @BindView(R.id.login_tv_phone_code)
    TextView login_tv_phone_code;
    @BindView(R.id.login_phone)
    EditText login_phone;
    @BindView(R.id.login_password)
    EditText login_password;
    @BindView(R.id.login_terms_of_service)
    CheckBox cb_terms_of_service;
    @BindView(R.id.but_login)
    Button   but_login;
    private Dialog mLoadingDialog;

    @Override
    protected void init() {
        PreferenceUtils.remove(SPKey.USER);
        PreferenceUtils.remove(SPKey.TOKEN);
        PreferenceUtils.remove(SPKey.USERID);
        mLoadingDialog = LoadingDialog.createLoadingDialog(this, "加载中", false);
        login_phone.addTextChangedListener(this);
        login_password.addTextChangedListener(this);
        but_login.setEnabled(false);
        PreferenceUtils.reset();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @NonNull
    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter(this, new ApiModel(this));
    }

    @OnClick({R.id.login_tv_phone_code, R.id.login_tv_phone_right, R.id.login_forget_password, R.id.but_login})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.login_tv_phone_code) {
            ActivityUtils.startActivityWithAnimotion(this, new Intent(this, CodeLoginActivity.class));
        } else if (view.getId() == R.id.login_tv_phone_right) {
            ActivityUtils.startActivityWithAnimotion(this, new Intent(this, RegisterActivity.class));
        } else if (view.getId() == R.id.login_forget_password) {
            ActivityUtils.startActivityWithAnimotion(this, new Intent(this, ForgetPasswordActivity.class));
        } else if (view.getId() == R.id.but_login) {
            if (!cb_terms_of_service.isChecked()) {
                MyToast.showToast(this, getString(R.string.red_terms_of_service));
                return;
            }
            getPresenter().login(login_phone.getText().toString(), login_password.getText().toString());
        }
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show) {
            mLoadingDialog.show();
        } else {
            mLoadingDialog.hide();
        }
    }

    @Override
    public void showError(String msg, String errorCode) {
        MyToast.showToast(this, msg);
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {

    }

    @Override
    public void showLogin() {
        EventUtil.postInfoEvent(EventConf.USER_CHANGE, 0);
        ActivityUtils.startActivityWithAnimotion(this, new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!login_phone.getText().toString().isEmpty() && Validator.isPassword(login_password.getText().toString())) {
            but_login.setEnabled(true);
        } else {
            but_login.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
